import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/bpm/system/menu'

const api = {

    /**
     * 新增菜单
     * @param menu
     * @returns {Promise<*|undefined>}
     */
    createMenu: (menu) =>
      axiosClient().post(`${requestUrl}/addInfo`, menu),

    /**
     * 根据ID删除菜单
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteMenuById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新菜单
     * @param id
     * @param menu
     * @returns {Promise<*|undefined>}
     */
    updateMenuFull: (id, menu) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, menu),

    /**
     * 根据ID部分更新菜单
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateMenuPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询菜单
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getMenuById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询菜单
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getMenuPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询菜单
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getMenuList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    },

    /**
     * 获取系统菜单
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getSystemMenu: () => axiosClient().get(`${requestUrl}/getSystemMenu`)

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.menuName) {
            params.append('menuName', businessParam.menuName)
        }
        if (businessParam.parentId) {
            params.append('parentId', businessParam.parentId)
        }
        if (businessParam.routePath) {
            params.append('routePath', businessParam.routePath)
        }
        if (businessParam.component) {
            params.append('component', businessParam.component)
        }
        if (businessParam.icon) {
            params.append('icon', businessParam.icon)
        }
        if (businessParam.menuType) {
            params.append('menuType', businessParam.menuType)
        }
        if (businessParam.permissionKey) {
            params.append('permissionKey', businessParam.permissionKey)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
